<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="6" :labelWidth="110" formType="add" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { addRole } from '@/api/common/auth/roleManage';
// import { getFjxmmcList, getSfqfdmList } from '@/api/repair/repairReception/customer/additional';
import * as types from '@/api/common/auth/roleManage/types';
import { addInsuranceRemind } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';

export default {
  name: 'AdditionalProaddform',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      index: null,
      formList: this.createFormList(),
      yearList: [],
      monthList: [],
      dayList: [],
      year: null
    };
  },
  computed: {
    ...mapState('app', ['btnLoading']),
    addBtnState() {
      return this.btnLoading[types.YAN_ROLE_ADD];
    }
  },
  created() {
    this.setData();
    var data = new Date();
    const year = data.getFullYear();
    for (let i = 1; i <= 12; i++) {
      this.monthList.push({ value: i, text: i });
    }
    for (let i = 1; i <= 31; i++) {
      this.dayList.push({ value: i, text: i });
    }
    for (let i = year - 3; i < year + 5; i++) {
      this.yearList.push({ value: i, text: i });
    }

    this.formList[4].itemList = this.yearList;
    this.formList[5].itemList = this.monthList;
    this.formList[6].itemList = this.dayList;
  },
  mounted() {},
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '保修公司',
          fieldName: 'insurationName',
          disabled: true,
          selfCols: 3
        },
        {
          type: 'DATE',
          label: '保险结束日期  ',
          fieldName: 'insuranceEndDate',
          disabled: true,
          selfCols: 3
        },
        {
          type: 'TEXT_AREA',
          label: '提醒内容',
          fieldName: 'remindContent',
          selfCols: 6,
          rows: 3
        },
        {
          type: 'TEXT_AREA',
          label: '客户反馈',
          fieldName: 'customerFeedback',
          selfCols: 6,
          rows: 3
        },
        {
          type: 'SELECT',
          label: '保险到期年',
          fieldName: 'expiredYear',
          selfCols: 2,
          itemList: [],
          change: val => {
            this.changegDay(val);
          }
        },
        {
          type: 'SELECT',
          label: '保险到期月',
          fieldName: 'expiredMonth',
          selfCols: 2,
          itemList: [],
          change: val => {
            this.changeMonth(val);
          }
        },
        {
          type: 'SELECT',
          label: '保险到期日',
          fieldName: 'expiredDay',
          selfCols: 2
        },
        {
          type: 'SELECT',
          itemList: this.createDictList('8083'),
          label: '购买意向',
          fieldName: 'purIntention',
          placeholder: '请选择',
          selfCols: 2
          // rules: [{ required: true, message: '请输入附加项目名称', trigger: 'blur' }],
          // change: val => {
          //   this.changeNameSelect(val);
          // }
        },
        {
          type: 'INPUT',
          label: '意向保险公司',
          fieldName: 'purCompany',
          selfCols: 2
        },
        {
          type: 'SELECT',
          itemList: [],
          label: '车身状态',
          fieldName: 'bodyState',
          placeholder: '请选择',
          selfCols: 2
        },
        {
          type: 'TEXT_AREA',
          label: '需求卖点',
          fieldName: 'sellingPoint',
          selfCols: 6,
          rows: 3
        },
        {
          type: 'SELECT',
          itemList: this.createDictList('8081'),
          label: '提醒方式',
          fieldName: 'remindWay',
          placeholder: '请选择',
          selfCols: 3
        },
        {
          type: 'INPUT',
          label: '提醒人',
          fieldName: 'reminder',
          selfCols: 3
        },
        {
          type: 'SELECT',
          itemList: this.createDictList('8057'),
          label: '提醒状态',
          fieldName: 'remindStatus',
          placeholder: '请选择',
          selfCols: 3
        },
        {
          type: 'SELECT',
          itemList: this.createDictList('8069'),
          label: '提醒失败原因',
          fieldName: 'remindFailReason',
          placeholder: '请选择',
          selfCols: 3
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 6,
          rows: 3
        },
        {
          type: 'CHECKBOX',
          itemList: this.createDictList('1004'),
          label: '是否续保战败',
          fieldName: 'isRenewlFailed',
          initialValue: 0,
          selfCols: 3,
          change: val => {
            this.show(val);
          }
        },
        {
          type: 'INPUT',
          label: '续保战败原因',
          fieldName: 'renewalFailedReason',
          selfCols: 3,
          disabled: true,
          rules: [{ required: true, message: '请选输入保战败原因', trigger: 'blur' }]
        },
        // {
        //   type: 'SELECT',
        //   itemList: this.createDictList('1393'),
        //   label: '续保战败原因',
        //   fieldName: 'RENEWAL_FAILED_REASON',
        //   placeholder: '请选择',
        //   selfCols: 3,
        //   rules: [{ required: true, message: '请选择续保战败原因', trigger: 'blur' }],
        // },
        {
          type: 'TEXT_AREA',
          label: '战败备注',
          fieldName: 'renewalRemark',
          selfCols: 6,
          disabled: true,
          rows: 3
        },
        {
          type: 'DATE',
          label: '续保战败日期',
          fieldName: 'renewalFailedDate',
          disabled: true,
          selfCols: 3,
          rules: [{ required: true, message: '请选输入续保战败日期', trigger: 'blur' }]
        }
      ];
    },
    setData() {
      console.log('setData', this.rowData);
      // for (let [key, value] of Object.entries(this.rowData)) {
      //   this.formList.some(item => {
      //     if (item.fieldName === key) {
      //       item.initialValue = value;
      //       return true;
      //     }
      //   });
      // }
      this.formList[0].initialValue = this.rowData.INSURATION_SHORT_NAME || null;
      this.formList[1].initialValue = this.rowData.INSURANCE_END_DATE || null;
      this.formList[4].initialValue = this.rowData.EXPIRED_YEAR || null;
      this.formList[5].initialValue = this.rowData.EXPIRED_MONTH || null;
      this.formList[6].initialValue = this.rowData.EXPIRED_DAY || null;
      this.formList[17].rules = [];
    },
    show(val) {
      if (val == 0) {
        this.formList[17].disabled = true;
        this.formList[18].disabled = true;
        this.formList[19].disabled = true;
        this.formList[17].initialValue = '';
        this.formList[19].initialValue = '';
        this.formList[17].rules = [];
        this.formList[19].rules = [];
      } else if (val == 1) {
        this.formList[17].disabled = false;
        this.formList[18].disabled = false;
        this.formList[19].disabled = false;
        this.formList[17].rules.push({ required: true, message: '请选输入保战败原因', trigger: 'blur' });
        this.formList[19].rules.push({ required: true, message: '请选输入保战败原因', trigger: 'blur' });
      }
    },
    changeMonth(val) {
      this.dayList = [];
      this.formList[6].itemList = [];

      // if(this.formList[6].initialValue=='' || this.formList[6].initialValue==null){
      //   return this.$notify.warning({title: '提示信息',message:"年份不能为空"})
      // }

      if (val == 1 || val == 3 || val == 3 || val == 5 || val == 7 || val == 8 || val == 10 || val == 12) {
        // 31天的月份
        for (let i = 1; i <= 31; i++) {
          this.dayList.push({ value: i, text: i });
        }
        this.formList[6].itemList = this.dayList;
      } else if (val == 4 || val == 6 || val == 9 || val == 11) {
        // 30天的月份
        for (let i = 1; i <= 30; i++) {
          this.dayList.push({ value: i, text: i });
        }
        this.formList[6].itemList = this.dayList;
      } else {
        if (this.formList[4].initialValue == '' || this.formList[4].initialValue == null) {
          return this.$notify.warning({ title: '提示信息', message: '年份为空,日期无联动' });
        } else {
          // 判断是否是闰年
          if (this.formList[4].initialValue % 4 != 0) {
            for (let i = 1; i <= 28; i++) {
              this.dayList.push({ value: i, text: i });
            }
            this.formList[6].itemList = this.dayList;
          } else {
            for (let i = 1; i <= 29; i++) {
              this.dayList.push({ value: i, text: i });
            }
            this.formList[6].itemList = this.dayList;
          }
        }
      }
    },
    changegDay(val) {
      // if(val==''||val==null){
      //     return this.$notify.warning({title: '提示信息',message:"年份为空,是否为闰年时候无联动效果"})
      // }
      if (val == '' || val == null) {
        return this.$notify.warning({ title: '提示信息', message: '年份为空,日期无联动' });
      } else {
        if (this.formList[5].initialValue == 2) {
          if (val % 4 != 0) {
            this.dayList = [];
            this.formList[6].itemList = [];
            for (let i = 1; i <= 28; i++) {
              this.dayList.push({ value: i, text: i });
            }
            this.formList[6].itemList = this.dayList;
          } else {
            this.dayList = [];
            this.formList[6].itemList = [];
            for (let i = 1; i <= 29; i++) {
              this.dayList.push({ value: i, text: i });
            }
            this.formList[6].itemList = this.dayList;
          }
        }
      }
    },
    openModel(data) {
      this.index = data.$index;
      // for (const item of this.formList) {
      //   item.initialValue = data[item.fieldName]
      // }
      this.formList[0].initialValue = data.ADD_ITEM_NAME;
      this.formList[1].initialValue = data.ADD_ITEM_CODE;
      this.formList[2].initialValue = data.ADD_ITEM_PRICE;
      this.formList[3].initialValue = data.CHARGE_PARTITION_CODE;
      this.formList[4].initialValue = data.REMARK;
      // console.log(this.formList)
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getFjxmmcList() {
      const res = await getFjxmmcList();
      // console.log(res)
      this.formList[0].itemList = res.data.map(x => ({ value: x.ADD_ITEM_NAME, text: x.ADD_ITEM_NAME, code: x.ADD_ITEM_CODE, price: x.ADD_ITEM_PRICE }));
    },
    async getSfqfdmList() {
      const res = await getSfqfdmList();
      // console.log(res)
      this.formList[3].itemList = res.data.map(x => ({ value: x.CHARGE_PARTITION_CODE, text: x.CHARGE_PARTITION_NAME }));
    },
    // async saveDialog() {
    //   this.$refs.formPanel.SUBMIT_FORM();

    //   this.$set(this.saveData, 'dataType', '10461001')
    //   this.$set(this.saveData, 'dataSource', '10451001')
    //   if (this.saveData !== null) {
    //     const res = await addRole(this.saveData);
    //     if (res.resultCode === 200) {
    //       this.$notify.success({title: '提示信息',message:'新增成功!'});
    //       // 重新加载
    //       this.closeDialog('refresh');
    //     }else {
    //       this.$notify.error({title: '提示信息',message:res.errMsg||'新增失败!'});
    //       this.closeDialog()
    //     }
    //   }
    // },
    saveDialog() {
      this.saveData = null;
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData != null) {
        // console.log("flag",flag,sum,this.saveData.receiveAmount)
        this.$confirm('是否确认保存?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let Data = _.cloneDeep(this.rowData);
            for (let [key, value] of Object.entries(this.saveData)) {
              Data.key = value;
            }
            let params = {
              ...Data
            };
            // console.log("params",params)
            let res = await addInsuranceRemind(params);
            // console.log('res',res)
            if (res.resultCode == 200) {
              this.$notify.success({ title: '提示信息', message: '保存成功!' });
              this.closeDialog();
            }
          })
          .catch(() => {});
      } else {
        this.$notify.error({ title: '提示信息', message: '有必填项未填写!' });
      }
    },
    closeDialog() {
      this.$emit('close', false);
    },
    changeNameSelect(val) {
      // console.log(this.formList)
      for (const item of this.formList[0].itemList) {
        if (item.text === val) {
          this.formList[1].initialValue = item.code;
          this.formList[2].initialValue = item.price;
        }
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
